/*
 * @(#) $Header$
 *
 * Copyright (C) 2007  Forklabs Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.media.jai;

import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.awt.image.renderable.RenderableImage;
import java.lang.reflect.Array;
import java.util.Collection;
import javax.media.jai.CollectionImage;
import javax.media.jai.OperationDescriptor;
import javax.media.jai.registry.CollectionRegistryMode;
import javax.media.jai.registry.RenderableCollectionRegistryMode;
import javax.media.jai.registry.RenderableRegistryMode;
import javax.media.jai.registry.RenderedRegistryMode;

/**
 * Class {@code DescriptorUtil} offers methods to help in the validation process
 * of {@link OperationDescriptor} objects.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.media.jai.DescriptorUtil">Daniel Léonard</a>
 * @version $Revision$
 */
public class DescriptorUtil {

//---------------------------
// Constructors
//---------------------------

   /**
    * Only allow subclasses.
    */
   protected DescriptorUtil() {
   // nothing
      }


//---------------------------
// Class methods
//---------------------------

   /**
    * Determines if the mode represents the rendered mode.
    * @param   mode   the mode.
    * @return   {@code true} if the mode is the same as
    *           {@link RenderedRegistryMode#MODE_NAME}, {@code false} otherwise.
    */
   public static boolean isRenderedMode(String mode) {
      String rendered_mode = RenderedRegistryMode.MODE_NAME;
      boolean is_rendered_mode = rendered_mode.equalsIgnoreCase(mode);
      return is_rendered_mode;
      }

   /**
    * Determines if the mode represents the renderable mode.
    * @param   mode   the mode.
    * @return   {@code true} if the mode is the same as
    *           {@link RenderableRegistryMode#MODE_NAME}, {@code false}
    *           otherwise.
    */
   public static boolean isRenderableMode(String mode) {
      String renderable_mode = RenderableRegistryMode.MODE_NAME;
      boolean is_renderable_mode = renderable_mode.equalsIgnoreCase(mode);
      return is_renderable_mode;
      }

   /**
    * Determines if the mode represents the collection mode.
    * @param   mode   the mode.
    * @return   {@code true} if the mode is the same as
    *           {@link CollectionRegistryMode#MODE_NAME}, {@code false}
    *           otherwise.
    */
   public static boolean isCollectionMode(String mode) {
      String collection_mode = CollectionRegistryMode.MODE_NAME;
      boolean is_collection_mode = collection_mode.equalsIgnoreCase(mode);
      return is_collection_mode;
      }

   /**
    * Determines if the mode represents the renderable collection mode.
    * @param   mode   the mode.
    * @return   {@code true} if the mode is the same as
    *           {@link RenderableCollectionRegistryMode#MODE_NAME},
    *           {@code false} otherwise.
    */
   public static boolean isRenderableCollectionMode(String mode) {
      String renderable_collection_mode = RenderableCollectionRegistryMode.MODE_NAME;
      boolean is_renderable_collection_mode = renderable_collection_mode.equalsIgnoreCase(mode);
      return is_renderable_collection_mode;
      }

   /**
    * Determines if the source is a rendered image.
    * @param   source   the source.
    * @return   {@code true} if the source is of type {@link RenderedImage},
    *           {@code false} otherwise.
    */
   public static boolean isSourceRendered(Object source) {
      boolean is_source_rendered = (source instanceof RenderedImage);
      return is_source_rendered;
      }

   /**
    * Determines if the source is a renderable image.
    * @param   source   the source.
    * @return   {@code true} if the source is of type {@link RenderableImage},
    *           {@code false} otherwise.
    */
   public static boolean isSourceRenderable(Object source) {
      boolean is_source_renderable = (source instanceof RenderableImage);
      return is_source_renderable;
      }

   /**
    * Determines if the source is a collection.
    * @param   source   the source.
    * @return   {@code true} if the source is of type {@link Collection},
    *           {@code false} otherwise.
    */
   public static boolean isSourceCollection(Object source) {
      boolean is_source_collection = (source instanceof Collection);
      return is_source_collection;
      }

   /**
    * Determines if the source is an array.
    * @param   source   the source.
    * @return   {@code true} if the source is an array, {@code false} otherwise.
    */
   public static boolean isSourceArray(Object source) {
      boolean is_source_array = source.getClass().isArray();
      return is_source_array;
      }

   /**
    * Determines if all the sources are rendered image.
    * @param   sources   the sources.
    * @return   {@code true} if all the sources are of type
    *           {@link RenderedImage}, {@code false} otherwise.
    */
   public static boolean areAllSourceRendered(Iterable<?> sources) {
      boolean are_rendered = true;
      for (Object source : sources) {
         are_rendered = DescriptorUtil.isSourceRendered(source);
         if (false == are_rendered) {
            break;
            }
         }
      return are_rendered;
      }

   /**
    * Determines if all the sources are renderable image.
    * @param   sources   the sources.
    * @return   {@code true} if all the sources are of type
    *           {@link RenderableImage}, {@code false} otherwise.
    */
   public static boolean areAllSourceRenderable(Iterable<?> sources) {
      boolean are_renderable = true;
      for (Object source : sources) {
         are_renderable = DescriptorUtil.isSourceRenderable(source);
         if (false == are_renderable) {
            break;
            }
         }
      return are_renderable;
      }

   /**
    * Collates all the sources into a single collection. All the original
    * sources are removed from the parameter block and the new
    * {@link CollectionImage} is put as <em>source0</em>. If a source is a
    * collection, all its elements are put in the order they are returned by the
    * iterator. If the source is an array, all the elements are put in
    * index-order.
    * @param   pb   the parameter block.
    */
   @SuppressWarnings("rawtypes")
   public static void collateSources(ParameterBlock pb) {
// BUG : should the collating process also collate each item (lists within lists) ?
      CollectionImage source0 = new SimpleCollectionImage();

      for (Object source : pb.getSources()) {
         boolean is_collection = DescriptorUtil.isSourceCollection(source);
         boolean is_array = DescriptorUtil.isSourceArray(source);
         if (is_collection) {
            source0.addAll((Collection) source);
            }
         else if (is_array) {
            Object array = source;
            int len = Array.getLength(array);
            for (int i = 0; i < len; i++) {
               source = Array.get(array, i);
               source0.add(source);
               }
            }
         else {
            source0.add(source);
            }
         }

      pb.removeSources();

      pb.addSource(source0);
      }

   }
